module Api
  class MerchantController < ApplicationController
    include Apiable

    def new
      params[:information_clerk_id] = api_current_user.id
      Merchant.create_with_images params

      data!
    end

    def index
      merchants = Merchant.select(:id, :name, :location, :created_at).
                          where(information_clerk_id: api_current_user.id)

      if params[:type] == "today"
        merchants = merchants.where("created_at >= ?", Date.today.beginning_of_day)
      end

      merchants = merchants.page(params[:page]).per(params[:page_size])

      data! merchants
    end

    def show
      merchant = Merchant.find_with_images(params[:id])

      data! merchant
    end

    def update
      Merchant.update_with_images params

      data!
    end
  end
end